﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

using System.Data;
using System.Configuration;
using System.Data.SqlClient;
namespace ScoreManage
{
    public partial class UserManage : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            BindGrid();
        }

        protected void BindGrid()
        {
            string strConn = ConfigurationManager.ConnectionStrings["studbConnectionString"].ConnectionString;
            SqlConnection conn = new SqlConnection(strConn);
            string strSql = "SELECT * FROM myuser";
            SqlCommand myCommand = new SqlCommand(strSql, conn);
            SqlDataAdapter da = new SqlDataAdapter(myCommand);
            DataSet ds = new DataSet();
            da.Fill(ds, "myuser");
            GridView1.DataSource = ds.Tables["myuser"];
            GridView1.DataBind();
        }

        protected void lbtnDelete_Click(object sender, EventArgs e)
        {
            string userid = ((LinkButton)sender).CommandArgument.ToString();
            string strConn = ConfigurationManager.ConnectionStrings["studbConnectionString"].ConnectionString;
            SqlConnection conn = new SqlConnection(strConn);
            conn.Open();
            string strSql = string.Format("DELETE myuser WHERE userid = '{0}'", userid);
            SqlCommand myCommand = new SqlCommand(strSql, conn);
            myCommand.ExecuteNonQuery();
            BindGrid();
        }

        protected void btnInsert_Click(object sender, EventArgs e)
        {
            Response.Redirect("UserInsert.aspx?item=5");
        }

        protected void btnUserid_Click(object sender, EventArgs e)
        {
            string Userid = txtQuery.Text.Trim().Replace("'", "''");
            string strConn = ConfigurationManager.ConnectionStrings["studbConnectionString"].ConnectionString;
            SqlConnection conn = new SqlConnection(strConn);
            string strSql = string.Format("SELECT * FROM myuser WHERE userid LIKE '%{0}%'", Userid);
            SqlCommand myCommand = new SqlCommand(strSql, conn);
            SqlDataAdapter da = new SqlDataAdapter(myCommand);
            DataSet ds = new DataSet();
            da.Fill(ds, "myuser");
            GridView1.DataSource = ds.Tables["myuser"];
            GridView1.DataBind();
        }

        protected void btnName_Click(object sender, EventArgs e)
        {
            string Name = txtQuery.Text.Trim().Replace("'", "''");
            string strConn = ConfigurationManager.ConnectionStrings["studbConnectionString"].ConnectionString;
            SqlConnection conn = new SqlConnection(strConn);
            string strSql = string.Format("SELECT * FROM myuser WHERE name LIKE '%{0}%'", Name);
            SqlCommand myCommand = new SqlCommand(strSql, conn);
            SqlDataAdapter da = new SqlDataAdapter(myCommand);
            DataSet ds = new DataSet();
            da.Fill(ds, "myuser");
            GridView1.DataSource = ds.Tables["myuser"];
            GridView1.DataBind();
        }
    }
}